cd ""

*use "data_main.dta", clear
import delimited "data_main.txt", encoding(ISO-8859-9) clear 
format date %td
tsset city date
gen ld=log(debit)
encode state, gen(ss)
encode fed, gen(ff)

tsset city a
gen dld=ld-l.ld
bys city: egen me=mean(debit)
gen mis=1 if debit==.
by city: egen tot_mis=max(mis)
 bys city: egen ab=min(dld)
  bys city: egen aa=max(dld)
 

gen debit2=debit+l.debit
gen debit_cum=debit if a==1
replace debit_cum=l.debit_cum+debit if a>1

keep if a==3 | a==5 | a==7 | a==9 | a==11 | a==13 | a==15 | a==17 | a==19 | a==21 | a==23 | a==25 | a==27 | a==29 | a==31 | a==33 | a==35 | a==37 | a==39 | a==41 | a==43 | a==45 | a==47 | a==49 | a==51 

replace a=a/2-0.5

tsset city a
gen ld2=log(debit2)
gen ld_cum=log(debit_cum)
char a[omit] 8
gen coef_sales=.
gen sd_sales=.


xi: reghdfe ld2 i.a*radio  c.a#c.urban c.a#c.d_old  c.a#c.black   if   ab>-1.0 & aa<1.0 & me<64725 & tot_mis!=1 & me>552 , absorb(cc ss#a ff#a) cluster(cc )

matrix A=e(b)
matrix B=e(V)

forval i=26(1)32 {

replace coef_sales=A[1,`i']  if a==-25+`i'
replace sd_sales=sqrt(B[`i',`i'])  if a==-25+`i'


}

replace coef_sales=0 if a==8
replace sd_sales=0 if a==8
forval i=33(1)51 {


replace coef_sales=A[1,`i']  if a==-24+`i'
replace sd_sales=sqrt(B[`i',`i'])  if a==-24+`i'


}



gen upperb_sales=coef_sales+1.65*sd_sales
gen lowerb_sales=coef_sales-1.65*sd_sales

gen upperb_sales5=coef_sales+1.96*sd_sales
gen lowerb_sales5=coef_sales-1.96*sd_sales

sort date
graph twoway   (rspike lowerb_sales5 upperb_sales5 date, color(gs12) mcolor(0)) (rcap lowerb_sales upperb_sales date, color(gs0) lwidth(medium) )  ///
 (scatter coef_sales date, color(gs0))   if a<22 & city==1 & a>1 , legend( label(1 "90%")  label(3 "Coefficient") label(2 "95%")  rows(1) order(3 2 1 ) ) ///
 title(Debits Flows, color(black)) ytitle(Log Debits) xtitle("") yline(0, lcolor(black)) xline(-9015, lcolor(black)) name(b2, replace) ///
  graphregion(color(white))   ysc(r(-0.6 0.6)) ylabel(-0.6(0.1)0.6) xsc(r(-9099   -8833 )) 
drop coef_sales sd_sales
drop upperb_sales lowerb_sales
drop upperb_sales5 lowerb_sales5



char a[omit] 8
gen coef_sales=.
gen sd_sales=.


xi: reghdfe ld_cum i.a*radio  c.a#c.urban c.a#c.d_old  c.a#c.black   if   ab>-1.0 & aa<1.0 & me<64725 & tot_mis!=1 & me>552 , absorb(cc ss#a ff#a) cluster(cc )

matrix A=e(b)
matrix B=e(V)

forval i=26(1)32 {

replace coef_sales=A[1,`i']  if a==-25+`i'
replace sd_sales=sqrt(B[`i',`i'])  if a==-25+`i'


}

replace coef_sales=0 if a==8
replace sd_sales=0 if a==8
forval i=33(1)51 {


replace coef_sales=A[1,`i']  if a==-24+`i'
replace sd_sales=sqrt(B[`i',`i'])  if a==-24+`i'


}



gen upperb_sales=coef_sales+1.65*sd_sales
gen lowerb_sales=coef_sales-1.65*sd_sales

gen upperb_sales5=coef_sales+1.96*sd_sales
gen lowerb_sales5=coef_sales-1.96*sd_sales

sort date
graph twoway   (rspike lowerb_sales5 upperb_sales5 date, color(gs12) mcolor(0)) (rcap lowerb_sales upperb_sales date, color(gs0) lwidth(medium) )  ///
 (scatter coef_sales date, color(gs0))   if a<22 & a>1 & city==1, legend( label(1 "90%")  label(3 "Coefficient") label(2 "95%")  rows(1) order(3 2 1 ) ) ///
 title(Cumulative Debits, color(black)) ytitle(Log Debits) xtitle("") yline(0, lcolor(black)) xline(-9015, lcolor(black)) name(b7, replace) ///
  graphregion(color(white) )   ysc(r(-0.2 0.2)) ylabel(-0.2(0.05)0.2) xsc(r(-9099   -8833 )) 
drop coef_sales sd_sales
drop upperb_sales lowerb_sales
drop upperb_sales5 lowerb_sales5
